//删除链表的倒数第 N 个结点
//https://leetcode.cn/problems/remove-nth-node-from-end-of-list/


/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* removeNthFromEnd(ListNode* head, int n) 
    {
        ListNode* newhead = new ListNode;
        newhead->next = head;

        int sum = 0;
        ListNode* cur = head;
        while(cur)
        {
            sum++;
            cur = cur->next;
        }

        ListNode* prev = newhead;

        cur = head;
        while(1)
        {
            cout << sum << endl;            if(sum == n) 
            {
                prev->next = cur->next;
                ListNode* ret = newhead->next;
                delete newhead;
                return ret;
            }
            prev = cur;
            cur = cur->next;
            sum--;
        }
    }
};